// Quark Api
// ---------------------------------------------------------------------------

(function(win){
    var host = ('https:' == document.location.protocol ? 'https://' : 'http://') + 'quark.dogannet.tv';

	// Player
	// -----------------------------------------------------------------------
	var player = function(){
		var opt, cnt, xdm, cfg, ready = false;
		var obj = {

			// Setup
			setup: function(options){
				if(!win.easyXDM){
					setTimeout(function(){ obj.setup(options); }, 500);
					return;
				}
				opt = $.extend(true, {
					player: {
						container: 'player',
						frame: true
					}
				}, options);				
				cnt = obj.container();
				xdm = new easyXDM.Socket({
					remote: host+'/player/frame.htm',
					container: cnt,
					props: {
						width: '100%',
						height: '100%',
						scrolling: 'no'
					},
					onReady: function(){
						xdm.postMessage(JSON.stringify({
							type: 'setup',
							content: opt
						}));
					},
					onMessage: function(message, origin){
						var message = JSON.parse(message);
						switch(message.type){
							case 'initialize':
								obj.initialize(message.content.config);
								break;	
							case 'debug':
								obj.debug(message.content.message);	
								break;													
							case 'error':
								obj.error(message.content.type);
								break;
							case 'mobile':
								obj.mobile(message.content.config);	
								break;														
							case 'callback':
								obj.callback(message.content.media, message.content.status);
								break;
						}
					}
				});
			},

			// Initialize
			initialize: function(config){
				if(opt.initialize) opt.initialize(config);
			},	

			// Debug
			debug: function (message){
				if(opt.debug) opt.debug(message);		
			},

			// Destroy
			destroy: function (message) {
				xdm.destroy();
			},

			// Error
			error: function (type){
				var emb = $('#'+opt.player.container+'-embed');
				var err = $('#'+opt.player.container+'-error');
				
				err.find("div[class*='error-']").hide();
				err.find('.error-'+type).show();
				
				if(xdm) xdm.destroy();
				emb.hide();
				err.show();
				if(opt.error) opt.error(type);		
			},								

			// Callback
			callback: function (media, status) {
				if(opt.callback) opt.callback(media, status);
			},

			// Trigger
			trigger: function (type){
				obj.error(type);
			},

			// Control
			control: function (control, param) {
				xdm.postMessage(JSON.stringify({
					type: 'control',
					content: control,
					param: param
				}));				
			},				

			// Container
			container: function(){
				var pre = opt.player.container;
				var cnt = $('#'+pre+'-container');

				if(!cnt.find('#'+pre+'-embed').length) cnt.append('<div id="'+pre+'-embed" style="width:100%; height:100%"></div>');
				if(!cnt.find('#'+pre+'-debug').length) cnt.append('<div id="'+pre+'-debug" style="width:100%; height:100%; display:none"></div>');
				if(!cnt.find('#'+pre+'-error').length) cnt.append('<div id="'+pre+'-error" style="width:100%; height:100%; display:none">'+
						'<div class="error-widevine">'+
							'Widevine Video Optimizer yÃƒÆ’Ã‚Â¼klÃƒÆ’Ã‚Â¼ deÃƒâ€žÃ…Â¸il! '+
							'<a target="_blank" href="http://tools.google.com/dlpage/widevine">Ãƒâ€¦Ã…Â¾imdi YÃƒÆ’Ã‚Â¼kle</a>'+
						'</div>'+
						'<div class="error-flash">'+
							'Flash Player yÃƒÆ’Ã‚Â¼klÃƒÆ’Ã‚Â¼ deÃƒâ€žÃ…Â¸il! '+
							'<a target="_blank" href="http://get.adobe.com/flashplayer">Ãƒâ€¦Ã…Â¾imdi YÃƒÆ’Ã‚Â¼kle</a>'+
						'</div>'+
					'</div>'
				);

				cnt.find("div[id^='"+pre+"-']").hide();
				cnt.find('#'+pre+'-embed').html('');
				cnt.find('#'+pre+'-embed').show();
				return pre+'-embed';
			},

			// Mobile
			mobile: function(config){
				var opt = config;
				var pre = opt.player.container;
				var emb = $('#'+pre+'-container').find('#'+pre+'-embed');
				var isHTML5 = (typeof(document.createElement('video').canPlayType) != 'undefined') ? true : false;

				// Player
				if(!$.player){
					var head = document.getElementsByTagName('head')[0];
					var link = document.createElement('link');
					link.rel = 'stylesheet';
					link.href = host+'/player/content/html5.css';
					head.appendChild(link);
					$.player = {
						callback: function(media, status){
							if(opt.callback) opt.callback(media, status)
						}
					}
				}
				
				// Embed
				emb.html('');
				if(isHTML5){
					if (emb.find('video').length > 0) return;
					var skip = !opt.advert.skipDisplay ? false : {
						displayText: "ReklamÃƒâ€žÃ‚Â± geÃƒÆ’Ã‚Â§",
						showDuration: opt.advert.skipShowDuration*1000
					};
					emb.qplayer({
						controls: true,
						autoplay: false,
						poster: opt.media.preview,
						src: opt.media.path,
						plugins: ['mobile_ads', 'event_tracker'],
						width: '100%',
						height:'100%',
						mobile_ads: {
							preroll: opt.advert.display ? opt.advert.preroll : null,
							countdown: true,
							skip: skip
						}
					});
				}
				else {
					if (opt.media.preview) {
						emb.html('<a href="' + opt.media.path + '"><img src="' + opt.media.preview + '" width="100%" height="100%" /></a>');
					} else {
						emb.html('<a href="' + opt.media.path + '"><b class="icon-netd-play"><i></i></b></a>');
					}
				}
			}
		};
		return obj;
	}

	// Stats
	// -----------------------------------------------------------------------
	var stats = function () {
		var url = 'http://stats.dogannet.tv/hit.ashx';
		var opt = {
            title: win.document.title,
			url: win.location.href,
			application: null,
			itemId: null
		};

		var obj = {
			hit: function (options) {
				opt = $.extend(true, opt, options);
				if (opt.application && opt.itemId) {
					$.ajax({
						type: "POST",
						url: url,
						data: opt,
						global: false,
						async: true,
						cache: false
					});
				}
			}
		}

		return obj;
	}

	// Quark
	// -----------------------------------------------------------------------	
	var quark = function(){
		var obj = {	

			// Init
			init: function(){							
				if(!win.jQuery){
					obj.loader(host+'/scripts/libs/jquery/jquery-min.js');
					return;
				}
				if(!win.JSON){
					obj.loader(host+'/scripts/libs/helper/json2.js');
					return;
				}
				if (!win.easyXDM) {
					obj.loader(host+'/scripts/libs/easyxdm/easyXDM.min.js');
					return;
				}
				/* Mobile
				if(!win.AdsManager){
					obj.loader(host+'/player/scripts/player.advert.js');
					return;
				}
				if(!win.qpLoad){
					obj.loader(host+'/player/scripts/player.html5.js');
					return;
				}
				*/
			},		

			// Loader
			loader: function(src){
				var head = document.getElementsByTagName('head')[0];
				var script = document.createElement('script');
				script.type = 'text/javascript';
				script.onreadystatechange= function () {
					if (this.readyState == 'complete' || this.readyState == 'loaded'){ 
						obj.init();
					}
				}
				script.onload = obj.init;
				script.src = src;
				head.appendChild(script);
			}
		}
		return obj;
	};

	// Init
	// -----------------------------------------------------------------------	
	win.quark = new quark();
	win.quark.host = host;
	win.quark.player = player;
	win.quark.stats = stats;
	win.quark.init();

})(window);